package com.appspot.mypartymanager.mbean;

import java.util.ArrayList;
import java.util.List;
import java.util.ResourceBundle;

import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
import javax.faces.context.FacesContext;
import javax.faces.event.ActionEvent;
import javax.servlet.http.HttpServletRequest;

import com.appspot.mypartymanager.entity.Party;
import com.appspot.mypartymanager.exception.InvalidPartyException;
import com.appspot.mypartymanager.service.PartyService;
import com.appspot.mypartymanager.service.impl.PartyServiceImpl;
import com.google.appengine.api.users.User;
import com.google.appengine.api.users.UserService;
import com.google.appengine.api.users.UserServiceFactory;

@ManagedBean(name = "partyBean")
@RequestScoped
public class PartyBean {

	private Party party;
	private PartyServiceImpl partyService;
	private User user;
	private ResourceBundle msg =  ResourceBundle.getBundle("com.appspot.mypartymanager.resources.messages");

	public PartyBean() {

		party = new Party();
		partyService = new PartyServiceImpl();
		UserService userService = UserServiceFactory.getUserService();
		user = userService.getCurrentUser();
		
	}
	
	public String create() {

		return "newParty";

	}
	
	public String view() {

		return "newParty";

	}
		
	public String edit() {

		return "newParty";

	}
	
	public void delete(ActionEvent actionEvent){

		partyService.deleteParty(party.getId());
		FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO, msg.getString("app.sucess"), msg.getString("entity.party.delete.sucess")));
  
	}
	
	public String cancel() {

		return "myParties";

	}
	
	/**
	 * create a new party
	 */
	public String createParty() {

		try {
			party.setOwnerGoogleAcount(user.getEmail());
			partyService.createParty(party);
			FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO, msg.getString("app.sucess"), msg.getString("entity.party.new.sucess")));
			
		} catch (InvalidPartyException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		return "myParties";

	}
	
	/**
	 * update party
	 */
	public String updateParty() {

		try {
			partyService.updateParty(party);
		} catch (InvalidPartyException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		return "myParties";

	}
	
	/**
	 * Parse texto with managers to List<String>
	 */
	public String getOtherManagers() {

		StringBuilder textListManagers = new StringBuilder();

		if (party.getId() != null) {

			List<String> listManagers = party.getPartyManagersGoogleAcounts();

			for (String manager : listManagers) {

				textListManagers.append(manager + ";");

			}

		}

		return textListManagers.toString();

	}
	
	public List<Party> getUserParties() {
		
		List<Party> userParties = new ArrayList<Party>();
		
		PartyService ps = new PartyServiceImpl();
		
		userParties = ps.getPartysByUser(user.getEmail());		
		
		return userParties;
	}

	public void setOtherManagers(String textListManagers) {

		List<String> listManagers = new ArrayList<String>();

		String[] arrayManagers = textListManagers.trim().split(";");

		if (arrayManagers.length > 0) {

			for (int i = 0; i < arrayManagers.length; i++) {

				listManagers.add(arrayManagers[i]);

			}

		}

		party.setPartyManagersGoogleAcounts(listManagers);

	}
	
	
	public void setParty(Party party) {
		this.party = party;
	}

	public Party getParty() {
		return party;
	}	
	
	
	
	
	
}
